{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, '../')\n",
    "%load_ext autoreload"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import imageio\n",
    "from glob import glob\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# covers of the top 100 movies on www.imdb.com/chart/top the 13th of August 2019\n",
    "# images downloaded from www.themoviedb.org\n",
    "data = []\n",
    "all_covers = glob('movie_covers/*.jpg')\n",
    "for cover_jpg in all_covers:\n",
    "    cover = imageio.imread(cover_jpg)\n",
    "    data.append(cover.reshape(np.prod(cover.shape)))\n",
    "    \n",
    "original_shape = imageio.imread(all_covers[0]).shape\n",
    "\n",
    "scaler = StandardScaler()\n",
    "data = scaler.fit_transform(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from minisom import MiniSom\n",
    "\n",
    "w = 10\n",
    "h = 10\n",
    "som = MiniSom(h, w, len(data[0]), learning_rate=0.5,\n",
    "              sigma=3, neighborhood_function='triangle')\n",
    "\n",
    "som.train_random(data, 2500, verbose=True)\n",
    "win_map = som.win_map(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "som.activation_response(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.axes_grid1 import ImageGrid\n",
    "%matplotlib inline\n",
    "\n",
    "fig = plt.figure(figsize=(30, 20))\n",
    "grid = ImageGrid(fig, 111,\n",
    "                 nrows_ncols=(h, w), axes_pad=0)\n",
    "\n",
    "def place_image(i, img):\n",
    "    img = (scaler.inverse_transform(img)).astype(int)\n",
    "    grid[i].imshow(img.reshape(original_shape))\n",
    "    grid[i].axis('off')\n",
    "\n",
    "to_fill = []\n",
    "collided = []\n",
    "\n",
    "for i in range(w*h):\n",
    "    position = np.unravel_index(i, (h, w))\n",
    "    if position in win_map:\n",
    "        img = win_map[position][0]\n",
    "        collided += win_map[position][1:]\n",
    "        place_image(i, img)\n",
    "    else:\n",
    "        to_fill.append(i)\n",
    "\n",
    "collided = collided[::-1]\n",
    "for i in to_fill:\n",
    "    position = np.unravel_index(i, (h, w))\n",
    "    img = collided.pop()\n",
    "    place_image(i, img)\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
